package com.terrier.ui.controls.supportClasses
{
    import com.terrier.ui.controls.WxButton;

    import flash.events.MouseEvent;

    public class WxSpinner extends WxRange
    {
        protected var increaseButton:WxButton;

        protected var decreaseButton:WxButton;

        private var _allowValueWrap:Boolean;

        public function WxSpinner()
        {
            super();
			createChildren();
        }

        protected function createChildren():void
        {
            increaseButton = new WxButton();
            increaseButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonDownHandler);
            addChild(increaseButton);
            decreaseButton = new WxButton();
            decreaseButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonDownHandler);
            addChild(decreaseButton);
        }

        public function get allowValueWrap():Boolean
        {
            return _allowValueWrap;
        }

        public function set allowValueWrap(value:Boolean):void
        {
            _allowValueWrap = value;
        }

        override public function changeValueByStep(increase:Boolean = true):void
        {
            if (allowValueWrap)
            {
                if (increase && (value == maximum))
                {
                    value = minimum;
                }
                else if (!increase && (value == minimum))
                {
                    value = maximum;
                }
                else
                {
                    super.changeValueByStep(increase);
                }
            }
            else
            {
                super.changeValueByStep(increase);
            }
        }

        private function buttonDownHandler(e:MouseEvent):void
        {
            var increase:Boolean = (e.target == increaseButton);
            changeValueByStep(increase);
        }
    }
}
